package com.distributed.common.message;

import net.sf.json.JSONObject;

import com.distributed.common.Constant;
import com.distributed.util.Encryptor;

public class LogoutMessage extends BaseMessage {
	public LogoutMessage() {
		super();
		setId(Constant.LOGOUT_MESSAGE);
	}

	public LogoutMessage(String username, String password) {
		super();
		setId(Constant.LOGOUT_MESSAGE);
		setUserName(username);
		setPassword(password);
	}
	
	public static String getName() {
		return Constant.LOGOUT_MESSAGE_NAME;
	}

	public void setUserName(String username) {
		argv.put(Constant.USER_NAME, username);
	}

	public String getUserName() {
		return argv.get(Constant.USER_NAME);
	}

	public void setPassword(String password) {
		argv.put(Constant.PASSWORD, Encryptor.MD5Encrytor(password));
	}

	public String getPassword() {
		return argv.get(Constant.PASSWORD);
	}
	
	public static LogoutMessage getFromString(String message) {
		JSONObject jsonobject = JSONObject.fromObject(message);
		if (jsonobject == null) {
			return null;
		}
		return getFromJSON(jsonobject);
	}

	public static LogoutMessage getFromJSON(JSONObject jsonobj) {
		BaseMessage baseMessage = new LogoutMessage();
		BaseMessage baseMsg = BaseMessage.getFromJSON(jsonobj, baseMessage);
		if (baseMsg == null || baseMsg.getId() != Constant.LOGOUT_MESSAGE) {
			return null;
		}
		
		return (LogoutMessage) baseMsg;
	}

	public JSONObject toJSON() {
		return super.toJSON();
	}
	
	public String toString() {
		return toJSON().toString();
	}
}
